package jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Random;

/**
 * 向student1表中插入100表数据。人名格式:test1---test100
 * 年龄10-15岁之间。班级号1-10之间
 *
 * 随机一个区间n,m   n:下线  m是上线
 * random.nextInt(m-n)+n   不包含上线值m
 * random.nextInt(m+1-n)+n 包含上限值m
 */
public class Test1 {
    public static void main(String[] args) throws ClassNotFoundException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        try(
                Connection connection = DriverManager.getConnection(
                        "jdbc:mysql://localhost:3306/tedu?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true",
                        "root",
                        "root");
        ) {
            Statement state = connection.createStatement();
            Random r = new Random();
            for(int i=1;i<=100;i++) {
                String name = "test"+i;
                int age = r.nextInt(6)+10;
                int classId = r.nextInt(10)+1;
                String sql = "INSERT INTO student1(name,age,class_id) " +
                        "VALUES('" + name + "',"+ age +"," + classId + ")";
                System.out.println(sql);
            /*
                int executeUpdate(String sql)
                专门用来执行DML语句的方法，返回值为执行该SQL后影响了表中多少条记录
             */
                int count = state.executeUpdate(sql);//INSERT插入1条数据
                if (count > 0) {//执行后至少影响了表中1条数据
                    System.out.println("影响了表中" + count + "条数据");
                }
            }
            System.out.println("执行完毕");
        }catch(SQLException e){
            e.printStackTrace();
        }
    }
}
